<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<link th:href="@{/static/bootstrap/css/bootstrap.min.css}" rel="stylesheet"/>
<link th:href="@{/static/bootstrap/editable/css/bootstrap-editable.css}" rel="stylesheet" />
<link th:href="@{/static/bootstrap/table/bootstrap-table.min.css}"  rel="stylesheet" />
<link th:href="@{/static/bootstrap/toastr/toastr.min.css}" rel="stylesheet"/>
</head>
<body>
<div class="row" style="margin-left:0px;margin-right:0px;">
<div class="col-sm-6" style="border:1px solid #ddd;border-radius:2px;">
	<div class="col-sm-12">
		<div class="btn-group-sm" id="toolbar" style="display: inline-flex;margin-top: 15px;">
		   <input id="searchValue" type="text" class="form-control" placeholder="输入表名或表描述信息">
			<a class="btn btn-success" style="margin-left: 10px;" onclick="javascript:searchTable()">
			 	<span class="glyphicon glyphicon-search" aria-hidden="true"></span>&nbsp;搜索
			</a>		     
			<a class="btn btn-success" style="margin-left: 10px;" id="showPath" onclick="javascript:showPath()">
			 	<span class="glyphicon glyphicon-road" aria-hidden="true"></span>&nbsp;路径
			</a>		     
			<a class="btn btn-success" style="margin-left: 10px;" id="showCollnum" onclick="javascript:showCollnum()">
			 	<span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span>&nbsp;字段
			</a>		     
			<a class="btn btn-success" style="margin-left: 10px;" id="createCode" onclick="javascript:createCode()">
			 	<span class="glyphicon glyphicon-floppy-saved" aria-hidden="true"></span>&nbsp;生成
			</a>		     
		</div>
	</div>	
    <div class="col-sm-12 select-table table-striped">
	    <table id="tables_table" data-mobile-responsive="true"></table>
	</div>
</div>
<div class="col-sm-6" style="border:2px solid #ddd;border-radius:5px;">
	<form id="tableForm">
	<div id="pannelright">
		<div class="panel panel-default" style="margin-top:13%;" id="pathPannel">
			<div class="panel-heading">
				包和类信息
			</div>
		    <div class="panel-body">
		      	<table class="table">
                    <tr>
                    	<td>路径</td>
                      <td>
                      	<input type="hidden" name="tableName" id="tableName"></input>
                        <input class="form-control" placeholder="路径" type="text" name="path" id="path" value="d:/generate">
                      </td>
                    </tr>
                    <tr>
                      <td>pojo</td>
                      <td colspan="2">
                        <input class="form-control" placeholder="pojo[包]名" type="text" name="beanPackageName" id="beanPackageName" value="com.lp.mgt.entities">
                      </td>
                      <td>
                        <input class="form-control" placeholder="pojo[类]名" type="text" name="beanName" id="beanName" value="Test">
                      </td>
                     </tr>
                    <tr>
                        <td>ctroller</td>
	                    <td colspan="2">
	                        <input class="form-control" placeholder="controller[包]名" type="text" name="controllerPkgName" id="controllerPkgName" value="com.lp.mgt.controllers">
	                    </td>
                        <td>
                         <input class="form-control" placeholder="controller[类]名" type="text" name="controllerName" id="controllerName" value="TestCtr">
                        </td>
                    </tr>                     
                     <tr>
                      <td>service</td>
                      <td colspan="2">
                        <input class="form-control" placeholder="service[包]名" type="text" name="servicePackageName" id="servicePackageName" value="com.lp.mgt.service">
                      </td>
                      <td>
                        <input class="form-control" placeholder="service[类]名" type="text" name="serviceName" id="serviceName" value="TestService">
                      </td>
                    </tr>
                     <tr>
                      <td>serviceImpl</td>
                      <td colspan="2">
                        <input class="form-control" placeholder="serviceImpl[包]名" type="text" name="serviceImplPackageName" id="serviceImplPackageName" value="com.lp.mgt.service.impls">
                      </td>
                      <td>
                        <input class="form-control" placeholder="serviceImpl[类]名" type="text" name="serviceImplName" id="serviceImplName" value="TestServiceImpl">
                      </td>
                    </tr>
                    <tr>
                      <td>mapper</td>
                      <td colspan="2">
                        <input class="form-control" placeholder="mapper[包]名" type="text" name="daoPackageName" id="daoPackageName" value="com.lp.mgt.mappers">
                      </td>
                      <td>
                        <input class="form-control" placeholder="mapper[类]名" type="text" name="daoName" id="daoName" value="TestMapper">
                      </td>
                    </tr>
                </table>  	
		    </div>
		</div>
		<div class="panel panel-default" style="margin-top:5%;" id="collnumPannel">
		  <div id="beanFileds" style="height:486px;overflow:auto;border:1px solid #ddd;border-radius:5px"> </div> 
		</div>
	</div>
</form>
</div>
</div>
<script th:src="@{/static/js/jquery-2.1.1.min.js}"></script>
<script th:src="@{/static/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/static/bootstrap/toastr/toastr.min.js}"></script>
<script th:src="@{/static/bootstrap/editable/js/bootstrap-editable.js}"></script>
<script th:src="@{/static/bootstrap/table/bootstrap-table.min.js}"></script>
<script th:src="@{/static/bootstrap/table/locale/bootstrap-table-zh-CN.min.js}"></script>
<script th:src="@{/static/bootstrap/table/extensions/editable/bootstrap-table-editable.min.js}"></script>
<script type="text/javascript">
//查询数据库中所有的表
$(function () {
	//隐藏详情信息
    $("#pathPannel").hide();
    $("#collnumPannel").hide();
   //表格的初始化
   $('#tables_table').bootstrapTable({
       method: 'post',                      //请求方式（*）
       url:"/generate/listDBTables",
       contentType: "application/x-www-form-urlencoded",
       dataType: 'json',
       toolbar: '#toolbar',                //工具按钮用哪个容器
       pagination: true,
       striped: true,                      //是否显示行间隔色
       cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
       pagination: true,                   //是否显示分页（*）
       sortable: false,                    //是否启用排序
       pageSize: 10,                       //每页的记录行数（*）
       pageNumber:1,						//默认第一页
       sortOrder: "asc",                   //排序方式
       height: 540,
        queryParams: function (params) {
       	 var param = {  
       			offset: params.offset,    
       			limit: params.limit,
       			page: (params.offset / params.limit) + 1,   //当前页码
                sortName: "createTime",
                sortOrder: "asc",
                searchValue:$("#searchValue").val(),
                };    
		return param;
       },                                  //传递参数（*）
       sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
       paginationLoop: true,
       pageList: [10, 25, 50],        //可供选择的每页的行数（*）
       search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
       strictSearch: true,
       showColumns: false,                  //是否显示所有的列
       showRefresh: false,                  //是否显示刷新按钮
       showSearch: false,
       minimumCountColumns: 2,             //最少允许的列数
       clickToSelect: true,                //是否启用点击选中行
       uniqueId: "uid",                     //每一行的唯一标识，一般为主键列
       showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
       cardView: false,                    //是否显示详细视图
	   selectItemName: 'parentItem',
       responseHandler: function (res) {	//结果集处理
           return eval(res.data)
       },
       
      columns: [{
    	  radio: true,
      },{
          title: "序号",
          align: 'center',
          formatter: function (value, row, index) {
        	  //获取每页显示的数量
              var pageSize=$('#tables_table').bootstrapTable('getOptions').pageSize;
              //获取当前是第几页
              var pageNumber=$('#tables_table').bootstrapTable('getOptions').pageNumber;
              //返回序号，注意index是从0开始的，所以要加上1
              return pageSize * (pageNumber - 1) + index + 1;
          }
      },{
          field: 'tableName',
          title: '表名',
          align: 'center',
      },{
          field: 'tableComment',
          title: '描述',
          align: 'center',
      },{
          field: 'createTime',
          title: '创建时间',
          align: 'center',
          sortable: true
      }/* ,{
          field: 'updateTime',
          title: '更新时间',
          sortable: true
      } *//* ,{
          title: '操作',
          align: 'center',
          formatter: function (value, row, index) {//这里的三个参数：value表示当前行当前列的值；row表示当前行的数据；index表示当前行的索引（从0开始）。
              var html = '<a type="button" onclick="viewModel(' + row.uid + ')"><span class="glyphicon glyphicon-eye-open" aria- hidden="true" ></span ></a>&nbsp;&nbsp;&nbsp;' +
                         '<a type="button" onclick="deleteModel(' + row.uid + ')"><span class="glyphicon glyphicon-trash" aria- hidden="true" ></span ></a>&nbsp;&nbsp;&nbsp;'+
                         '<a type="button" onclick="editModel('+row.uid+')"><span class="glyphicon glyphicon-edit" aria- hidden="true" ></span ></a>';
              return html;
          }
      } */
      ],
      onClickRow:function (row) {//行点击事件
    	$("#tableName").val(row.tableName);//表名字段
    	if(((row.tableName.split('_')).length-1)>1){
    		$("#createCode").attr('disabled',"true");
    		$("#showPath").attr('disabled',"true");
    		$("#showCollnum").attr('disabled',"true");
    	}else{
    		$("#createCode").removeAttr("disabled");
    		$("#showPath").removeAttr("disabled");
    		$("#showCollnum").removeAttr("disabled");
    	}
        var beanFields = $("#beanFileds");
        beanFields.html("");
        $.ajax({
            type : 'get',
            url : '/generate',
            data : "tableName=" + row.tableName,
            success : function(data) { 
                var beanName = data.beanName;
                $("#beanName").val(beanName);
                $("#daoName").val(beanName + "Mapper");
                $("#serviceName").val(beanName + "Service");
                $("#serviceImplName").val(beanName + "ServiceImpl");
                $("#controllerName").val(beanName + "Ctr");
                var fields = data.fields;
                var length = fields.length;
                html_tr="<table class='table'>"+
                		"<thead><tr>"+
	                        "<th>表字段名</th>"+
	                        "<th>表类型</th>"+
	                        "<th>java字段名</th>"+
	                        "<th>java字段类型</th>"+
                    	"</tr></thead><tbody>"
                for(var i=0;i<length;i++){
                	var f=fields[i];
                	var columnName = f['columnName'];
                	var name = f['name'];
                	var type = f['type'];
                	var tr="<tr>"+
			                	"<td><input type='hidden' name='columnNames' value='" + f['columnName'] +"'>"+f['columnName']+"</td>"+
			                	"<td>" + f['columnType'] + "</td>"+
			                	"<td><input name='beanFieldName' class='form-control' value='" + f['name'] +"'></td>"+
			                	"<td><input name='beanFieldType' class='form-control' value='" + f['type'] +"'></td>"+
			                	"<td><input type='hidden' name='beanFieldValue' class='form-control' value='"+ f['columnDefault'] +"'></td>"+
			                "</tr>"
                	html_tr=html_tr+tr;
                }
               	html_tr=html_tr+"</tbody></table>"
                beanFields.html(html_tr);
            }
        });
      }
   });
});

function searchTable(){
	$('#tables_table').bootstrapTable('refresh');  
}

function showPath(){
	var row=$("#tables_table").bootstrapTable('getSelections');
	if(row.length<1)return;
    $("#pathPannel").show();
    $("#collnumPannel").hide();
}

function showCollnum(){
	var row=$("#tables_table").bootstrapTable('getSelections');
	if(row.length<1)return;
    $("#pathPannel").hide();
    $("#collnumPannel").show();
}

//生成代码
function createCode() {
	debugger;
	var datas=$("#tableForm").serialize();
    $.ajax({
        type : 'post',
        url : '/generate',
        //contentType: "application/json; charset=utf-8",  
        data :datas,
        success : function(data) {
        	toastr.success("代码生成成功","提示");
        }
    });
}

</script>
</body>
</html>